﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;

namespace DacSanMienTay
{
    public class Utility
    {
        public static string modHost = ConfigurationSettings.AppSettings["ModHost"];
        public static string userHost = ConfigurationSettings.AppSettings["UserHost"];
        public static int CharsPerProductName = int.Parse(ConfigurationSettings.AppSettings["Product.CharsPerProductName"]);
        public static String ConvertTo_NoSoundVietnamese(string accented)
        {
            if (accented == null || accented == "")
                return "";

            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");

            string strFormD = accented.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        public static String RemoveSpecialChars(String str)
        {
            StringBuilder buffer = new StringBuilder(str.Length);
            foreach (char ch in str)
                if (('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || (ch == '-' || ch == '_' || ch == ' '))
                    buffer.Append(ch);
            return buffer.ToString();
        }
        public static string sefWord(String str)
        {
            if (str.Length > CharsPerProductName)
                str = str.Substring(0, CharsPerProductName);

            return HttpUtility.UrlEncode(RemoveSpecialChars(ConvertTo_NoSoundVietnamese(str).ToLower().Trim().Replace(' ', '-')));
        }

        public static string sefRestaurantLink(int partnerID, string partnerName)
        {
            return String.Format(userHost + "/nha-hang-ngon/{0}-{1}.html", partnerID, sefWord(partnerName).ToLower());

        }

        public static string sefRestaurantLinkNew(int partnerID, string partnerName)
        {
            return String.Format(userHost + "/nha-hang-ngon/{0}-{1}.html", partnerID, sefWord(partnerName).ToLower());

        }
    }
}